package com.css.fxfzypg.modules.model.controller;

import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.logInfo.SysLog;
import com.css.fxfzypg.modules.model.entity.Casualty;
import com.css.fxfzypg.modules.model.entity.DataPbeInjuries;
import com.css.fxfzypg.modules.model.service.CasualtyService;
import com.css.fxfzypg.modules.model.to.CasualtyTO;
import com.css.fxfzypg.modules.model.vo.CalculateVO;
import com.css.fxfzypg.modules.model.vo.CasualtyVO;
import com.css.fxfzypg.util.PlatformPageUtils;
import com.css.fxfzypg.util.ResponseCreator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2022年09月27日  17:15
 * 人员伤亡计算模型
 */
@RestController
@Validated
@RequestMapping("/casualty")
public class CasualtyController {

    @Resource
    private CasualtyService casualtyService;

    /***
     * 人员伤亡计算模型列表分页查询
     * @param casualtyTO
     */
    @SysLog("人员伤亡计算模型列表分页查询")
    @GetMapping("/page")
    public RestResponse getPage(CasualtyTO casualtyTO, HttpServletRequest request){
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=casualtyService.getPage(casualtyTO,curPage,pageSize);
            return ResponseCreator.createSuccessResponse(page,"查询成功!");
    }

    /***
     * 人员伤亡计算模型列表分页查询
     * 现场灾害所用
     * @param casualtyTO
     */
    @SysLog("人员伤亡计算模型列表分页查询")
    @GetMapping("/pageForXczh")
    public RestResponse getPageForXczh(CasualtyTO casualtyTO, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=casualtyService.getPageForXczh(casualtyTO,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page,"查询成功!");
    }
    /***
     * 人员伤亡计算模型根据模型id查询信息
     * @param id
     */
    @SysLog("根据模型id查询人员伤亡计算模型信息")
    @GetMapping("/getModelById")
    public RestResponse getModelById(@NotBlank(message="模型id不能为空") @RequestParam("id") String id){
        Casualty model = casualtyService.getModelById(id);
        return ResponseCreator.createSuccessResponse(model);
    }

    /***
     * 人员伤亡计算模型根据模型id查询信息用于复制信息回显
     * @param id
     */
    @GetMapping("/getModelByIdForCopy")
    public RestResponse getModelByIdForCopy(@NotBlank(message="模型id不能为空") @RequestParam("id") String id){
        CasualtyVO model = casualtyService.getModelByIdForCopy(id);
        return ResponseCreator.createSuccessResponse(model);
    }

    /***
     * 人员伤亡计算模型根据模型-用于复制保存
     * @param casualty
     */
    @SysLog("复制保存人员伤亡计算模型根据模型")
    @PostMapping("/save")
    public RestResponse save(@Valid @RequestBody Casualty casualty){
        casualtyService.save(casualty);
        return ResponseCreator.createSuccessResponse("","保存成功");
    }

    /***
     * 人员伤亡计算模型根据模型-修改
     * @param casualty
     */
    @SysLog("修改人员伤亡计算模型根据模型")
    @PostMapping("/update")
    public RestResponse update(@Valid @RequestBody Casualty casualty){
        casualtyService.update(casualty);
        return ResponseCreator.createSuccessResponse("","保存成功");
    }

    /***
     * 人员伤亡计算模型根据模型-删除
     * @param to
     */
    @SysLog("删除人员伤亡计算模型根据模型")
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody CasualtyTO to){
        casualtyService.delete(to.getId());
        return ResponseCreator.createSuccessResponse("","删除成功");
    }

    /***
     * 详情页面计算时默认显示的模型信息
     * @param
     */
    @GetMapping("/getDefaultModel")
    public RestResponse getDefaultModel(@NotBlank(message = "任务id不能为空") @RequestParam String taskId){
        Casualty casualty = casualtyService.getDefaultModel(taskId);
        return ResponseCreator.createSuccessResponse(casualty);
    }

    /**
     * 计算区域下人员伤亡3
     * @param vo
     * @return
     */
    @PostMapping("/calculate")
    public RestResponse calculate(@RequestBody  CalculateVO vo) {
        RestResponse restResponse= null;
        try {
            restResponse = casualtyService.calculate(vo.getTaskId(),vo.getRange());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return restResponse;
    }

    /**
     * 计算格网下的人员伤亡2
     * @param vo
     * @return
     */
    @PostMapping("/savePbeInjuries")
    public RestResponse savePbeInjuries(@RequestBody  CalculateVO vo) {
        try {
            return casualtyService.savePbeInjuries(vo.getModeId(),vo.getTaskId());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(503,e.getMessage());
        }
    }

    /**
     * 导出各烈度下人员伤亡
     * @param
     * @return
     */
    @GetMapping("/exportPbeInjuries")
    public void exportPbeInjuries(String taskId, HttpServletResponse resp){
        try {
            //  return casualtyService.countIntensityAndStructureOfArea((String) map.get("taskId"),(String) map.get("tab"),(String) map.get("range"));
            casualtyService.exportPbeInjuries(taskId,resp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出各烈度下人员伤亡数据失败");
        }
    }
    /**
     * 计算各烈度下各类建筑物的破坏面积1
     * @param
     * @return
     */
    @PostMapping("/countIntensityAndStructureOfArea")
    public RestResponse countIntensityAndStructureOfArea(@RequestBody CalculateVO vo){
        try {
          //  return casualtyService.countIntensityAndStructureOfArea((String) map.get("taskId"),(String) map.get("tab"),(String) map.get("range"));
            return casualtyService.countIntensityAndStructureOfArea(vo.getTaskId(),vo.getTab(),vo.getRange());
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.fail(503,e.getMessage());
        }
    }

    /**
     * 导出各烈度下各类建筑物的破坏面积1
     * @param
     * @return
     */
    @GetMapping("/exportIntensityAndStructureOfArea")
    public void exportIntensityAndStructureOfArea( String taskId, HttpServletResponse resp){
        try {
            //  return casualtyService.countIntensityAndStructureOfArea((String) map.get("taskId"),(String) map.get("tab"),(String) map.get("range"));
            casualtyService.exportIntensityAndStructureOfArea(taskId,resp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出各烈度下建筑物破坏数据失败");
        }
    }

    /**
     * @author Dongbj
     * @date 2022/11/28 11:01
     * @desc  评估结果人员伤亡分布图
     */
    @SysLog("查询评估结果人员伤亡分布图")
    @GetMapping("getPbeInjuriesByTaskIdAndIntensity")
    public RestResponse getPbeByTaskIdAndIntensity(@NotBlank(message = "任务id不能为空") @RequestParam String taskId,
                                                   @NotBlank(message = "地震烈度不能为空") @RequestParam String dzIntensity){
        List<DataPbeInjuries> pbeList = casualtyService.getPbeByTaskIdAndIntensity(taskId,dzIntensity);
        return ResponseCreator.createSuccessResponse(pbeList);
    }
}
